// pages/recruitHome/recruitHome.js
var fileData = require('../../utils/data.js');
var main = require('../../utils/main.js');
const util = require('../../utils/util.js')
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    httpApiImg: app.globalData.httpApiImg,
    banner_url: [],
    bottabData: app.globalData.bottabDataForZp,
    interval: 3000,
    duration: 1000,
    vertical: false,
    indicatordots: true,
    autoplay: true,
    indicatored: '#F08631',
    indicator: '#fff',
    imageUrl: app.globalData.imageUrl,
    curNavId: 1,
    curIndex: 0,
    // advert_url: fileData.getAdvertData(),
    actiItem: [],
    bannerAdvert: [],
    page: 1,
    focus: false,
    seachValue: '',
    isFullJob:true,
    partJobList:[],
    myIsRcruit:false,
    currentPage:1,
    currentPage1:1,
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData: true,
    hasData1: true,
    loadSuccess: false
  },
  inputFocus: function () {//搜索框聚焦事件
    this.setData({
      focus: true
    })
  },
  inputBlur: function () {//搜索框失去焦点事件
    this.setData({
      focus: false
    })
  },
  changeInput: function (e) {//搜索框值改变事件
    this.setData({
      seachValue: e.detail.value
    })
    if (this.data.myIsRcruit) {
      this.getRecruitPersonalInfo(true);
    } else {
      this.getSeachData(true);
    }
  },
  tabSwitch:function(e){
    this.setData({
      isFullJob: !this.data.isFullJob
    })
    if (!this.data.isFullJob && this.data.hasData1) {
      this.getSeachData(true);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    fileData.getBannerData.apply(this, ['5', 'top', 0, 'banner_url']);
    fileData.getBannerData.apply(this, ['5', 'top', 1, 'bannerAdvert']);
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      ['bottabData[0].current']: true
    })
    if (this.data.myIsRcruit){
      wx.setNavigationBarTitle({
        title:'求职'
      })
    }
    
    this.getUserIdentity();
    wx.showLoading();
  },
  getUserIdentity: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/getOne', //仅为示例，并非真实的接口地址
      // method: 'POST',
      // header: {
      //   'content-type': 'application/x-www-form-urlencoded' // 默认值
      // },
      data: { userId: this.data.userInfo.id },
      success: function (res) {
        if (res.data.success) {
          if (res.data.data){
            that.setData({
              myIsRcruit: res.data.data.userCharacter =='招聘'?true:false
            })
            // that.setData({
            //   myIsRcruit: true
            // })
            if (that.data.myIsRcruit){
              that.getRecruitPersonalInfo(true);
            }else{
              that.getSeachData(true);
            }
          } else {//设置用户身份
            that.setUserIdentity();
          }
        }
      }
    })
  },
  setUserIdentity: function () {//设置用户身份
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/save', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: { userId: this.data.userInfo.id, user_character:'求职'},
      success: function (res) {
        if (res.data.success) {
          that.setData({
            myIsRcruit: false
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  switchBotab(e) {
    if (e.currentTarget.dataset.index != 0) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    }
  },
  enterPerson: function (e) {
    wx.navigateTo({
      url: '/pages/personalProfile/personalProfile?id=' + e.target.id
    })
  },
  getRecruitPersonalInfo: function (flag){
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/page', //仅为示例，并非真实的接口地址
      data: {
        userCharacter: '求职',
        currentPage: this.data.currentPage,
        pageNumber: 10,//this.data.seachValue
        jobName: this.data.seachValue
      },
      success: function (res) {
        if (res.data.success) {
          wx.hideLoading();
          that.setData({
            loadSuccess: true
          })
          if (res.data.data.pp.length < 10) {
            that.setData({
              hasData: false
            })
          } else {
            that.setData({
              hasData: true
            })
          }
          res.data.data.pp.forEach(function (ele) {
            if (ele.createTime) {
              ele.time = util.formatDate(ele.createTime);
            }
            if(ele.enterWorkDate){
              var year = new Date(ele.enterWorkDate.replace(/\-/g, "/"));
              var now=new Date();
              var workYear = parseInt(now - year) / 1000 / 3600 / 24 / 365;
              ele.workYear = workYear.toFixed(0);
            }
          })
          if (flag) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            that.setData({
              actiItem: []
            });
          } else {
            that.setData({
              loadMore: false
            });
          }
          that.data.actiItem = that.data.actiItem.concat(res.data.data.pp);
          that.setData({
            actiItem: that.data.actiItem
          })
        }
      }
    })
  },
  getSeachData(flag) {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPositionInfo/page', //仅为示例，并非真实的接口地址
      data: { 
        property: this.data.isFullJob ? '全职' :'兼职',
        currentPage: this.data.isFullJob ? this.data.currentPage : this.data.currentPage1,
        pageNumber:10,
        name: this.data.seachValue
      }, 
      success: function (res) {
        wx.hideLoading();
        that.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          if (res.data.data.pp.length < 10) {
            that.setData({
              [that.data.isFullJob ? 'hasData' : 'hasData1']: false
            })
          } else {
            that.setData({
              [that.data.isFullJob ? 'hasData' : 'hasData1']: true
            })
          }
          res.data.data.pp.forEach(function(ele){
            if (ele.createTime){
              ele.time = util.formatDate(ele.createTime);
            }
            if (ele.workPlace){
              ele.area = ele.workPlace.split(',')[1];
            }
          })
          if (flag) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            that.setData({
              [that.data.isFullJob ? 'actiItem' :'partJobList']: []
            });
          } else {
            that.setData({
              loadMore: false
            });
          } 
          if (that.data.isFullJob){
            that.data.actiItem = that.data.actiItem.concat(res.data.data.pp);
            that.setData({
              actiItem: that.data.actiItem
            })
          }else{
            that.data.partJobList = that.data.partJobList.concat(res.data.data.pp);
            that.setData({
              partJobList: that.data.partJobList
            })
          }
        }
      }
    })
  },
  showDetail: function (e) {
    var item = e.currentTarget.dataset.item;
    if (this.data.myIsRcruit){
      wx.navigateTo({
        url: '/pages/resumeDetail/resumeDetail?id=' + item.id + '&userId=' + item.userId
      })
    }else{
      // this.addBrowseNum(item.id);
      wx.navigateTo({
        url: '/pages/recruitDetail/recruitDetail?id=' + item.id
      })
    }
  },
  // addBrowseNum:function(id){
  //   wx.request({
  //     url: app.globalData.httpApiCar + 'recruitPositionInfo/addBrowseNum', //仅为示例，并非真实的接口地址
  //     method: 'POST',
  //     header: {
  //       'content-type': 'application/x-www-form-urlencoded' // 默认值
  //     },
  //     data: { id: id},
  //     success: function (res) {}
  //   })
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  switchTap: function (e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      [this.data.isFullJob ? 'currentPage' : 'currentPage1']: 1
    })
    
    if (this.data.myIsRcruit){
      this.getRecruitPersonalInfo(true);
    }else{
      this.getSeachData(true);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasData) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        [this.data.isFullJob ? 'currentPage' : 'currentPage1']: this.data.isFullJob ? this.data.currentPage + 1 : this.data.currentPage1+1
      });
      if (this.data.myIsRcruit) {
        this.getRecruitPersonalInfo();
      } else {
        this.getSeachData();
      }
      // this.getSeachData();
    }
  },
})